package com.logos.controller;

import com.logos.entity.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/save")
    @ResponseBody
    public String save() {
        System.out.println("user save ...");
        return "{'userController':'save'}";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public String delete() {
        System.out.println("user delete...");
        return "{'userController':'delete'}";
    }

    // 普通参数
    @RequestMapping("/commonParam")
    @ResponseBody
    public String commonParam(String name, int age) {
        System.out.println("user commonParam run ... name:" + name + ",age:" + age);
        return "{'userController':'commonParam'}";
    }

    // 普通参数:请求参数名与形参名不同
    @RequestMapping("/commonParamDifferentName")
    @ResponseBody
    public String commonParamDifferentName(@RequestParam(value = "name") String userName, @RequestParam("age") int age) {
        System.out.println("user commonParamDifferentName run ... userName:" + userName + ",age:" + age);
        return "{'userController':'commonParamDifferentName'}";
    }

    // pojo参数
    @RequestMapping("/pojoParam")
    @ResponseBody
    public String pojoParam(User user) {
        System.out.println("user pojoParam run ... name:" + user.getName() + ",age:" + user.getAge());
        return "{'userController':'pojoParam'}";
    }

    // 嵌套pojo参数
    @RequestMapping("/pojoContainPojoParam")
    @ResponseBody
    public String pojoContainPojoParam(User user) {
        System.out.println("user pojoContainPojoParam run ... param:" + user);
        return "{'userController':'pojoContainPojoParam'}";
    }

    // 数组参数
    @RequestMapping("/arrayParam")
    @ResponseBody
    public String arrayParam(String[] likes) {
        System.out.println("user arrayParam run ... param:" + likes.toString());
        return "{'userController':'arrayParam'}";
    }

    // 集合参数
    @RequestMapping("/listParam")
    @ResponseBody
    public String listParam(@RequestParam List<String> likes) {
        System.out.println("user listParam run ... param:" + likes.toString());
        return "{'userController':'listParam'}";
    }

    // 集合参数 json格式
    @RequestMapping("/listParamForJson")
    @ResponseBody
    public String listParamForJson(@RequestBody List<String> likes) {
        System.out.println("user listParamForJson run ... param:" + likes.toString());
        return "{'userController':'listParamForJson'}";
    }

    // pojo参数 json格式
    @RequestMapping("/pojoParamForJson")
    @ResponseBody
    public String pojoParamForJson(@RequestBody User user) {
        System.out.println("user pojoParamForJson run ... param:" + user);
        return "{'userController':'pojoParamForJson'}";
    }

    // 集合参数 json格式
    @RequestMapping("/listPojoParamForJson")
    @ResponseBody
    public String listPojoParamForJson(@RequestBody List<User> list) {
        System.out.println("user listPojoParamForJson run ... param:" + list);
        return "{'userController':'listPojoParamForJson'}";
    }

    // 日期参数
    @RequestMapping("/dataParam")
    @ResponseBody
    public String dataParam(@DateTimeFormat(pattern = "yyy/MM/dd") Date date1,
                            @DateTimeFormat(pattern = "yyy-MM-dd") Date date2,
                            @DateTimeFormat(pattern = "yyy/MM/dd HH:mm:ss") Date date3) {
        System.out.println("user dataParam run ... param:" + date1 + "," + date2 + "," + date3);
        return "{'userController':'dataParam'}";
    }

    // 响应页面/跳转页面
    @RequestMapping("/toJumpPage")
    public String toJumpPage() {
        System.out.println("跳转页面");
        return "page.jsp";
    }

    // 响应文本数据
    @RequestMapping("/toText")
    @ResponseBody
    public String toText() {
        System.out.println("返回纯文本数据");
        return "response text";
    }


    // 响应POJO对象
    @RequestMapping("/toJsonPOJO")
    @ResponseBody
    public User toJsonPOJO() {
        System.out.println("返回json对象数据");
        User user = new User();
        user.setName("bsJTB");
        user.setAge(19);
        return user;
    }

    // 响应POJO集合对象
    @RequestMapping("/toJsonPOJOList")
    @ResponseBody
    public List<User> toJsonPOJOList() {
        System.out.println("返回json集合数据");
        User user1 = new User();
        user1.setName("bsJTB");
        user1.setAge(19);

        User user2 = new User();
        user2.setName("xlJTB");
        user2.setAge(19);
        List<User> userList = new ArrayList<>();
        userList.add(user1);
        userList.add(user2);
        return userList;
    }

}
